package com.juicygames.shmup.player;

import android.util.Log;
import android.view.MotionEvent;

import com.juicygames.framework.GameMath;
import com.juicygames.framework.gamepart.GameHandler;
import com.juicygames.framework.property.space.Position;
import com.juicygames.framework.touch.EntityTouchAction;
import com.juicygames.framework.touch.WorldTouchAction;
import com.juicygames.framework.type.Point;
import com.juicygames.shmup.playerbullet.PlayerBullet;

public class PlayerTouchHandler extends GameHandler<EntityTouchAction> {
	private static final String LOGTAG = PlayerTouchHandler.class.getName();
	
	private Point startFlickPoint;
	
	private static final float BULLET_SPEED = 6;
	
	public Class<EntityTouchAction> getActionClassHandled() {
		return EntityTouchAction.class;
	}
	
	public void handle(EntityTouchAction touch) {
//		Log.d(LOGTAG,"omg1 touchy touchy at " + touch.getX() + "," + touch.getY());
		
		MotionEvent event = touch.getEvent();
		
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			startFlickPoint = new Point(touch.getX(), touch.getY());
		}
	}
	
	//hack
	public void handleGlobalTouchRelease(WorldTouchAction touch) {
		if (startFlickPoint != null) {
			
			Point endPoint = touch.getPoint();
			
			PlayerBullet bullet = getWorld().addPart(new PlayerBullet());
			
			bullet.getPart(Position.class).set(new Point(startFlickPoint));
			
			bullet.getPart(Mover.class).setVelTowardPoint(endPoint, (int) (GameMath.distance(startFlickPoint, endPoint) / BULLET_SPEED));
		}
		
		startFlickPoint = null;
	}
	

}
